<template>
  <div class="login">
    <el-card>
      <img class="img" src="../../assets/logo.png" alt="" />
      <!-- 基本表单验证 el-form 绑定 model,rules -->
      <el-form :model="form" ref="form" :rules="rules">
        <!-- el-form-item 绑定 prop -->
        <el-form-item prop="mobile">
          <!-- 表单元素绑定 v-model -->
          <el-input placeholder="请输入手机号" v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input placeholder="请输入验证码" v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass"
            >我已阅读并同意[用户协议]和[隐私条款]</el-checkbox
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { authorizationsPost } from '@/api/index.js'
import { setItem } from '@/utils/auth.js'
export default {
  data () {
    return {
      form: {
        mobile: '13200000000',
        code: '246810',
        isPass: []
      },
      rules: {
        //   prop的值:[{required:true(*是否为必填,否false),message:"错误信息",trigger:'change/blur'},
        //    {min:最小长度,max:最大长度,message:"错误信息",trigger:'出发时机(change改变时/blur失去焦点时)'},
        //    {自定义验证规则: validator:(rule,value,callback)=>{成功:callback():失败 callback(new Error(错误信息))}} ]
        mobile: [
          { required: true, message: '手机号必填', trigger: 'blur' },
          {
            validator: (rule, value, callbcak) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callbcak()
              } else {
                callbcak(new Error('请输入正确的手机号'))
              }
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '验证码必填', trigger: 'blur' },
          {
            min: 6,
            max: 6,
            message: '验证码长度必须六位',
            trigger: 'blur'
          }
        ],
        isPass: [{ required: true, message: '必选', trigger: 'blur' }]
      }
    }
  },
  methods: {
    submit () {
      // 全局表单验证
      this.$refs.form.validate(async result => {
        if (result) {
          const res = await authorizationsPost(this.form)
          console.log('登录信息', res)
          setItem(res.data.token)
          this.$message.success('登陆成功')
          this.$router.push('/home')
          // authorizationsPost(this.form).then(res => {
          //   console.log('登录信息', res)
          // })
        } else {
          // this.$message.error('验证失败')
        }
      })
    }
    // 局部表单验证(){
    // this.$refs.form.validateField(['prop的值'],(errorMessage)=>{errorMessge:错误信息 (有错误:验证失败 为空:验证成功))})
    // }
  }
}
</script>

<style lang="less" scoped>
.login {
  height: 100%;
  background-image: url('../../assets/login.png');
  background-size: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .el-card {
    width: 460px;
    .img {
      display: block;
      width: 200px;
      margin: 0 auto;
    }
    .btn {
      width: 100%;
    }
    // scoped 看得到的都能管,看不见的都管不到
    //看不见的想管 ::v-deep
    ::v-deep .el-card__body {
      padding: 20px 40px;
    }
  }
}
</style>
